import java.net.MalformedURLException;
import java.net.URL;
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.browser.ProgressEvent;
import org.eclipse.swt.browser.ProgressListener;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.mozilla.interfaces.nsIDOMDocument;
import org.mozilla.interfaces.nsIDOMHTMLDocument;
import org.mozilla.interfaces.nsIDOMWindow;
import org.mozilla.interfaces.nsIWebBrowser;

/**
 * This class contain the main method to launch the application and also all the
 * GUI widget construction
 */
public class MainScreen {
	private static final String DEFAULT_URL = "http://www25.discuss.com.hk/archiver/?fid-194.html";

	/**
	 * Contruct the GUI widgets
	 * 
	 * @param args
	 *            Can put the first parameter as URL
	 */
	public static void main(final String[] args) {
		final Display display = new Display();
		final FormToolkit toolkit = new FormToolkit(display);
		final Shell shell = new Shell(display);
		shell.setLayout(new GridLayout(2, true));
		final String url = args.length > 0 ? args[0] : DEFAULT_URL;

		final Text txtSearch = toolkit.createText(shell, url, SWT.BORDER);
		final Browser browser = new Browser(shell, SWT.MOZILLA);
		final Button dumpDom = toolkit.createButton(shell, "dump dom", SWT.NONE);
		final Button saveScrom = toolkit.createButton(shell, "save scrom", SWT.NONE);

		txtSearch.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(final KeyEvent event) {
				if (event.keyCode == SWT.CR || event.keyCode == SWT.KEYPAD_CR) {
					browser.setUrl(txtSearch.getText());
				}
			}
		});

		browser.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true, 2, 1));
		browser.setUrl(url);

		setupBrowser(browser, dumpDom, saveScrom);
		shell.open();
		while (!shell.isDisposed()) {
			try {
				if (!display.readAndDispatch()) {
					display.sleep();
				}
			} catch (final RuntimeException e) {
				e.printStackTrace();
			}
		}
		display.dispose();
	}

	/**
	 * Initalize the event listener
	 * 
	 * @param browser
	 * @param dumpDom
	 * @param saveScrom
	 */
	private static void setupBrowser(final Browser browser, final Button dumpDom, final Button saveScrom) {
		browser.addProgressListener(new ProgressListener() {

			public void changed(final ProgressEvent event) {
			}

			public void completed(final ProgressEvent event) {
				final nsIWebBrowser webBrowser = (nsIWebBrowser) browser.getWebBrowser();
				final nsIDOMWindow contentDOMWindow = webBrowser.getContentDOMWindow();
				final nsIDOMDocument doc = contentDOMWindow.getDocument();
				dumpDom.addSelectionListener(new DumpDomAdapter(doc.getChildNodes(), System.out));
				try {
					saveScrom.addSelectionListener(new SaveScromAdaptor(new URL(browser.getUrl()),
							(nsIDOMHTMLDocument) doc.queryInterface(nsIDOMHTMLDocument.NS_IDOMHTMLDOCUMENT_IID)));
				} catch (final MalformedURLException e) {
					throw new RuntimeException(e);
				}
			}
		});
	}

}